import {defineStore} from "pinia";
import {getSysRoleList, postSysRole, putSysRole, putSysRoleDoAssign} from "@/api/role";
import type {TRoleInfo, TRoleParams} from "@/api/role/type.ts";
import {ElMessage} from "element-plus";

type TState = {
    roleList:TRoleInfo[],
    keyword:string
}
const useRoleStore = defineStore("Role",{
    state():TState{
        return {
            roleList:[],
            keyword:""
        }
    },
    actions:{
        // 添加权限
        async postSysRoleAsync(body:TRoleParams){
            const {message} = await postSysRole(body);
            ElMessage.success(message);
            await this.getSysRoleListAsync();
        },
        // 修改权限
        async putSysRoleAsync(body:TRoleParams){
            const {message} = await putSysRole(body);
            ElMessage.success(message);
            await this.getSysRoleListAsync();
        },
        async getSysRoleListAsync(){
            const result = await getSysRoleList(this.keyword);
            this.roleList = result.roleList;
        },
        // 分配权限
        async putSysRoleDoAssignAsync(id:string,body:string[]){
            const {message} = await putSysRoleDoAssign(id,body);
            ElMessage.success(message);
            await this.getSysRoleListAsync();
        }
    }
});
export default useRoleStore;